{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "76d45aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f77495a",
   "metadata": {},
   "source": [
    "Import the needed tools."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "689b3ef9",
   "metadata": {},
   "outputs": [],
   "source": [
    "file0=pd.read_csv('gaze.csv')\n",
    "file0=np.array(file0)\n",
    "file1=pd.read_csv('gaze.csv')\n",
    "file1=np.array(file1)\n",
    "file2=pd.read_csv('gaze.csv')\n",
    "data1=[]\n",
    "datax=[]\n",
    "datay=[]\n",
    "data2=[]\n",
    "data22=[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ef8eb485",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n"
     ]
    }
   ],
   "source": [
    "for item in file1:\n",
    "    sh= item[2]\n",
    "    if sh>0.9:\n",
    "        data1.append(item)\n",
    "question1=len(data1)\n",
    "print(question1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98a21bbf",
   "metadata": {},
   "source": [
    "The question1 is the outcome of the first question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2549a175",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "86185\n"
     ]
    }
   ],
   "source": [
    "for i in data1:\n",
    "    datax.append(i[3])\n",
    "    datay.append(i[4])\n",
    "\n",
    "ymean1 = np.mean(datax)\n",
    "ystd1 = np.std(datax)\n",
    "down1 = ymean1 - 3 * ystd1\n",
    "up1 = ymean1 + 3 * ystd1\n",
    "ymean2 = np.mean(datay)\n",
    "ystd2 = np.std(datay)\n",
    "down2 = ymean2 - 3 * ystd2\n",
    "up2 = ymean2 + 3 * ystd2\n",
    "\n",
    "for i in data1:\n",
    "    if (i[3] > down1) and (i[3] < up1):\n",
    "        data2.append(i)\n",
    "    else:\n",
    "        continue\n",
    "for i in data2:\n",
    "    if (i[4] > down2) and (i[4] < up2):\n",
    "        data22.append(i)\n",
    "    else:\n",
    "        continue\n",
    "question2=len(data22)\n",
    "print(question2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d117dc6b",
   "metadata": {},
   "source": [
    "The question2 is the outcome of the second question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9aba67d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          gaze_timestamp  world_index  confidence  norm_pos_x  \\\n",
      "0       1970-01-02T 00:29:10.776780+0000            0    0.828006    0.531433   \n",
      "1       1970-01-02T 00:29:10.776787+0000            0    0.459705    0.337051   \n",
      "2       1970-01-02T 00:29:10.779709+0000            0    0.888789    0.543140   \n",
      "3       1970-01-02T 00:29:10.779759+0000            0    0.550258    0.337923   \n",
      "4       1970-01-02T 00:29:10.787485+0000            1    0.502595    0.340109   \n",
      "...                                  ...          ...         ...         ...   \n",
      "125259  1970-01-02T 00:34:03.695600+0000         8788    0.995336    0.407831   \n",
      "125260  1970-01-02T 00:34:03.697570+0000         8788    0.948733    0.407552   \n",
      "125261  1970-01-02T 00:34:03.701580+0000         8788    0.945284    0.407467   \n",
      "125262  1970-01-02T 00:34:03.703608+0000         8788    0.945284    0.407160   \n",
      "125263  1970-01-02T 00:34:03.705574+0000         8788    0.945284    0.407637   \n",
      "\n",
      "        norm_pos_y                           base_data  gaze_point_3d_x  \\\n",
      "0         0.397509                       88150.77678-0        28.369753   \n",
      "1         0.186147                      88150.776787-1      -142.869866   \n",
      "2         0.410668                      88150.779709-0        38.942051   \n",
      "3         0.173301                      88150.779759-1      -141.879169   \n",
      "4         0.142989                      88150.787485-1      -139.404860   \n",
      "...            ...                                 ...              ...   \n",
      "125259    0.413470        88443.69561-0 88443.695591-1       327.648915   \n",
      "125260    0.414731   88443.69561-0 88443.69953099999-1       311.054477   \n",
      "125261    0.414705  88443.703629-0 88443.69953099999-1       313.566509   \n",
      "125262    0.415340       88443.703629-0 88443.703587-1       297.019831   \n",
      "125263    0.413674  88443.703629-0 88443.70751899999-1       323.629974   \n",
      "\n",
      "        gaze_point_3d_y  gaze_point_3d_z  eye_center0_3d_x  ...  \\\n",
      "0             52.033135       705.121116         20.000000  ...   \n",
      "1            154.788121       684.981621        -39.934928  ...   \n",
      "2             45.359863       705.232112         20.000000  ...   \n",
      "3            160.867779       683.893108        -39.934928  ...   \n",
      "4            175.088321       681.151453        -39.934928  ...   \n",
      "...                 ...              ...               ...  ...   \n",
      "125259      -173.027524     -2777.243340         19.888755  ...   \n",
      "125260      -161.381388     -2628.637408         19.888755  ...   \n",
      "125261      -162.585330     -2647.420719         19.888755  ...   \n",
      "125262      -152.352987     -2499.428189         19.888755  ...   \n",
      "125263      -170.142461     -2737.404466         19.888755  ...   \n",
      "\n",
      "        eye_center0_3d_z  gaze_normal0_x  gaze_normal0_y  gaze_normal0_z  \\\n",
      "0             -20.000000        0.011527        0.051002        0.998632   \n",
      "1             -20.075283       -0.141761        0.192518        0.971000   \n",
      "2             -20.000000        0.026087        0.041811        0.998785   \n",
      "3             -20.075283       -0.140397        0.200891        0.969501   \n",
      "4             -20.075283       -0.136989        0.220475        0.965725   \n",
      "...                  ...             ...             ...             ...   \n",
      "125259        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125260        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125261        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125262        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125263        -20.707271       -0.111009        0.041794        0.992940   \n",
      "\n",
      "        eye_center1_3d_x  eye_center1_3d_y  eye_center1_3d_z  gaze_normal1_x  \\\n",
      "0                    NaN               NaN               NaN             NaN   \n",
      "1                    NaN               NaN               NaN             NaN   \n",
      "2                    NaN               NaN               NaN             NaN   \n",
      "3                    NaN               NaN               NaN             NaN   \n",
      "4                    NaN               NaN               NaN             NaN   \n",
      "...                  ...               ...               ...             ...   \n",
      "125259        -37.372594         14.763454        -23.294339       -0.130868   \n",
      "125260        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125261        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125262        -37.372594         14.763454        -23.294339       -0.133311   \n",
      "125263        -37.372594         14.763454        -23.294339       -0.131320   \n",
      "\n",
      "        gaze_normal1_y  gaze_normal1_z  \n",
      "0                  NaN             NaN  \n",
      "1                  NaN             NaN  \n",
      "2                  NaN             NaN  \n",
      "3                  NaN             NaN  \n",
      "4                  NaN             NaN  \n",
      "...                ...             ...  \n",
      "125259        0.093021        0.987026  \n",
      "125260        0.091860        0.986979  \n",
      "125261        0.091860        0.986979  \n",
      "125262        0.091656        0.986827  \n",
      "125263        0.092941        0.986974  \n",
      "\n",
      "[125264 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "def timestyle(timecode):\n",
    "    timeStamp=timecode\n",
    "    dateArray = datetime.datetime.fromtimestamp(timeStamp,datetime.timezone.utc)\n",
    "    otherStyleTime = dateArray.strftime(\"%Y-%m-%dT %H:%M:%S.%f%z\")\n",
    "    return otherStyleTime\n",
    "file2[\"gaze_timestamp\"]=file2[\"gaze_timestamp\"].apply(lambda x: timestyle(x))\n",
    "print(file2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ae9731e",
   "metadata": {},
   "source": [
    "The file2 is the outcome of the question3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0f1ed1ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "427.62610766083765\n"
     ]
    }
   ],
   "source": [
    "rate=len(file0)/float(file0[-1][0]-file0[0][0])\n",
    "print(rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65b945f8",
   "metadata": {},
   "source": [
    "The rate is outcome of the question4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "97fc121b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88150.77678, 0, 0.8280059462620806, 0.5314327126985153, 0.397509462115002, '88150.77678-0', 28.36975290157324, 52.033134560158175, 705.1211159195539, 20.0, 15.0, -20.0, 0.0115267687421528, 0.0510017897651116, 0.998632039864094, nan, nan, nan, nan, nan, nan]\n",
      "[88150.787485, 1, 0.5025948800258426, 0.3401089118572799, 0.1429894950624901, '88150.787485-1', -139.4048601388959, 175.088321362755, 681.1514528333081, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1369893374305413, 0.2204754503935745, 0.9657248558487516, nan, nan, nan, nan, nan, nan]\n",
      "[88150.7956825, 1, 0.7063552247599557, 0.4939747472555963, 0.4084287570104117, '88150.79569-0 88150.795675-1', 2.226757768794034, -19.036159854780287, -288.7272253410736, 20.0, 15.0, -20.0, 0.0651451604743196, 0.0273741459204546, 0.9975002577452808, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1530847520875171, 0.2209875356663993, 0.9631871924804624]\n",
      "[88150.80726949999, 1, 0.8563407607235008, 0.4947773641166069, 0.4049685390557781, '88150.809749-0 88150.80479-1', 1.925754580585762, -19.710658057686643, -288.0721152448382, 20.0, 15.0, -20.0, 0.066337739085217, 0.0238399079900352, 0.997512387472, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1522284421706256, 0.2298142020041658, 0.96125539475807]\n",
      "[88150.8159975, 2, 0.8361660611207393, 0.4936290323359708, 0.4005829406173053, '88150.81599799999-0 88150.815997-1', 2.3733773877048456, -20.832665442340023, -291.0391610701988, 20.0, 15.0, -20.0, 0.0639621055325309, 0.0248933367913213, 0.9976418048775022, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1520938716848489, 0.2339803621189008, 0.960271130638957]\n",
      "[88150.827729, 2, 0.9151973420751922, 0.4935611049606477, 0.399305458229399, '88150.827729-0 88150.827729-1', 2.429836652393641, -21.37441266274697, -294.81920004608054, 20.0, 15.0, -20.0, 0.0628834658754589, 0.0250997423608705, 0.9977052032804624, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1502426207035535, 0.2341154968216942, 0.9605295878170852]\n",
      "[88150.83570099999, 2, 0.8981226615721961, 0.4931131952439982, 0.3989479733166602, '88150.83570099999-0 88150.83570099999-1', 2.580643834051319, -21.2999141960879, -292.7523092034187, 20.0, 15.0, -20.0, 0.0628194739502493, 0.0271573900279123, 0.9976553462290908, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1518890946821565, 0.2334673795780245, 0.9604283864971952]\n",
      "[88150.84749249999, 3, 0.8146346350814423, 0.4926846570563509, 0.3989208383436903, '88150.85153599999-0 88150.84344899999-1', 2.776177148200823, -21.57727727360416, -296.4848556983736, 20.0, 15.0, -20.0, 0.0612830191043649, 0.0256685125875367, 0.9977903181686012, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1505621238051983, 0.2334480372562587, 0.960642004482663]\n",
      "[88150.85654, 3, 0.8028764861728308, 0.4927015882674469, 0.3985696259880583, '88150.85654-0 88150.85654-1', 2.766044192974574, -21.623266864181776, -296.08798529724027, 20.0, 15.0, -20.0, 0.0614032164203954, 0.0259169127990371, 0.9977765073623442, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1507301965157871, 0.2338791905465301, 0.960510766252837]\n",
      "[88150.86601499999, 3, 0.922197307158175, 0.4925920221557682, 0.3987046665407951, '88150.867514-0 88150.86451599999-1', 2.8279107518092026, -21.7509379548824, -298.2332454694398, 20.0, 15.0, -20.0, 0.0607170218320272, 0.0252198938208978, 0.9978363594375146, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1498142804573954, 0.2335102349998405, 0.9607438011880964]\n",
      "[88150.87752149999, 4, 0.94500047108518, 0.4920943029105782, 0.3989300839628567, '88150.879586-0 88150.87545699999-1', 3.018227170856065, -21.704819872309287, -298.2646502338695, 20.0, 15.0, -20.0, 0.0600403618646908, 0.024936294324659, 0.9978844302686116, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1504509230615827, 0.2334355900843485, 0.9606624511408208]\n",
      "[88150.8860805, 4, 0.9336578810932824, 0.4921659182867304, 0.3991673717146227, '88150.88389-0 88150.88827099999-1', 2.993379604392894, -21.671916187220244, -298.5133295164393, 20.0, 15.0, -20.0, 0.0600744566094347, 0.0244272104483781, 0.997894969950642, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1502346770715741, 0.2334867407864019, 0.9606838625074032]\n",
      "[88150.8975135, 4, 0.8550647363113325, 0.4915064547947733, 0.3989299375954948, '88150.89551599999-0 88150.899511-1', 3.2536311073442707, -21.77831974188079, -299.2742419323912, 20.0, 15.0, -20.0, 0.0589982936701745, 0.024598649249555, 0.9979549628110008, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1507240075641575, 0.2333606522117972, 0.9606378503593768]\n",
      "[88150.90740649999, 4, 0.860093311910097, 0.491088842773452, 0.3988032611048965, '88150.90741599999-0 88150.90739699999-1', 3.4153029561257977, -21.8164544627108, -299.42300047452966, 20.0, 15.0, -20.0, 0.058394789018706, 0.0240004762718797, 0.998005022910298, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1511843810946026, 0.2340633438184941, 0.9603945199726764]\n",
      "[88150.917009, 5, 0.8383589621749243, 0.4916988785237518, 0.3969829040783048, '88150.91700999999-0 88150.91700799999-1', 3.205992735337801, -22.379871794808, -301.7281257296696, 20.0, 15.0, -20.0, 0.0586426228150969, 0.0253109034231119, 0.9979581158331612, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1492563387270982, 0.2345754008162153, 0.9605711460801296]\n",
      "[88150.9275245, 5, 0.853040894539125, 0.4914520921772419, 0.3974846823427633, '88150.927522-0 88150.92752699999-1', 3.323771199109265, -22.42242492444516, -303.7814987183897, 20.0, 15.0, -20.0, 0.0578151068830742, 0.0239351244254893, 0.9980403414866736, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1486016593137565, 0.2343942415664973, 0.9607168606669008]\n",
      "[88150.9361805, 5, 0.9976716979910984, 0.4910973671158898, 0.3962520166514687, '88150.93622799999-0 88150.936133-1', 3.4655296294769027, -22.7171236500922, -304.1173390246404, 20.0, 15.0, -20.0, 0.0572557360018466, 0.0255675734157424, 0.998032103634206, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1489008775061077, 0.2344783973497013, 0.9606499934181164]\n",
      "[88150.947523, 6, 0.8036163788282745, 0.4916238935759408, 0.3961178880738129, '88150.947523-0 88150.947523-1', 3.262791263322139, -22.761999060445, -304.32465222130315, 20.0, 15.0, -20.0, 0.0579144157958601, 0.0258493072210173, 0.9979868404739706, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1481174755850684, 0.2343310605809422, 0.9608070396668196]\n",
      "[88150.9554835, 6, 0.8788892496462954, 0.4913386350072072, 0.3977555249534704, '88150.955483-0 88150.95548399999-1', 3.3662896201724912, -22.352572589595265, -303.6373329086299, 20.0, 15.0, -20.0, 0.0576983716468707, 0.0237021050495953, 0.9980526579923118, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1488208667257802, 0.2342770447397616, 0.9607115154587204]\n",
      "[88150.9674385, 6, 0.8927541387709003, 0.4908699620824434, 0.3966637456195263, '88150.96744-0 88150.967437-1', 3.5720576637630472, -22.74160554313528, -305.6581007674199, 20.0, 15.0, -20.0, 0.0565841376898487, 0.024528738222491, 0.9980964764806606, -39.93492801030824, 14.997919452545707, -20.075282531198983, -0.1484760207186315, 0.2343051011491903, 0.9607580292909492]\n"
     ]
    }
   ],
   "source": [
    "full=[]\n",
    "outcome=[]\n",
    "start_time=file0[0][0]\n",
    "for i in file0:\n",
    "    full.append(i[0])\n",
    "for j in file0:\n",
    "    t=min(full, key=lambda x: abs(x - start_time))\n",
    "    if j[0]==t:\n",
    "        outcome.append(j.tolist())\n",
    "        start_time += 0.01\n",
    "    # Because the data is so big, the time maybe a little long(three hours).\n",
    "    # So we can use the below to see the data:\n",
    "    if len(outcome)==20:\n",
    "       break\n",
    "    if len(outcome)>100*float(file0[-1][0]-file0[0][0]):\n",
    "        break\n",
    "for i in outcome:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "011b46be",
   "metadata": {},
   "source": [
    "The outcome is question 5."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
